package com.example.kpopnews.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;
import com.example.kpopnews.utils.Utils;

//*************************************************
public final class ServiceUtils
// *************************************************
{
	public static final String URL = "http://www.quanlytiendo.com";

	public static final String RESULT_OK = "OK";
	public static final String ERROR_OUT_OF_MEMORY = "<<<";

	public static final String USERNAME = "UserName";
	public static final String PASSWORD = "Password";

	// *************************************************
	public static String doPost(String methodName,
			List<NameValuePair> nameValuePairs)
	// *************************************************
	{
		String url = URL + "/User/" + methodName + ".aspx";
		// if (AppMain._user.getDomain() != null
		// && !AppMain._user.getDomain().trim().equals(""))
		// {
		// url = URL + "/" + AppMain._user.getDomain() + "/User/" + methodName
		// + ".aspx";
		// }

		// AppMain.debug(url + nameValuePairs.toString());

		String responseString = null;

		Utils.debug("POST 11111111111111");
		// Creating HTTP client
		HttpClient httpClient = new DefaultHttpClient();

		Utils.debug("POST 22222222222");
		// Creating HTTP Post
		HttpPost httpPost = new HttpPost(url);

		Utils.debug("POST 33333333333");
		// URL Encoding the POST parameters
		try
		{
			if (methodName.equalsIgnoreCase("SaveCommentJobItem"))
				httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
						"utf-8"));
			else
				httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		} catch (OutOfMemoryError e)
		{
			e.printStackTrace();
			httpPost = null;
			httpClient = null;
			nameValuePairs = null;
			responseString = null;
			System.gc();
			return ERROR_OUT_OF_MEMORY;
		}

		Utils.debug("POST 4444444444444");
		// Making HTTP Request
		try
		{
			HttpResponse response = httpClient.execute(httpPost);

			// writing response to log
			Log.d("Http Response.getStatusLine:",
					response.getStatusLine().toString());

			responseString = OpenHttpConnection.getResponseBody(response);

			Log.d("Http Response:", "" + responseString);
		} catch (ClientProtocolException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		Utils.debug("POST OK!!!!!!!!!");
		return responseString;
	}

	// *************************************************
	public static String doLogin(String username, String password)
	// *************************************************
	{
		String methodName = "CheckUserName";

		// Building post parameters: key and value pair
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
		nameValuePair.add(new BasicNameValuePair(USERNAME, username));
		nameValuePair.add(new BasicNameValuePair(PASSWORD, password));

		return doPost(methodName, nameValuePair);
	}

}
